<?php

namespace Admin\Controller;
use Think\Controller;

class AgentController extends BaseController{
	
	public function index(){
		$admin = session('admin');
		$integral = M('integral');
		$agent = M('agent');
		$order = M('order');
		$user = M('user');
		$start = I('starttime');
		$end = I('endtime');
		if($strat){
			$map['time'] = array('egt',strtotime($start));
		}
		if($end){
			$map['time'] = array('elt',strtotime($end));
		}
		if($start && $end){
			$map['time'] = array(array('egt',strtotime($start)),array('elt',strtotime($end)),'AND');
		}
		
		if($admin['role_id']==0){//管理员
			$count = $agent->where($map)->count();
			$page = new \Think\Page($count,10);
			$show = $page->show();
			$list = $agent->where($map)->limit($page->firstRow.','.$page->listRows)->select();
			for($i=0;$i<count($list);$i++){
				if($list[$i]['agent_rank']==1){
					$agent_list_2 = $agent->where("agent_up = {$list[$i]['id']}")->select();
					for($a=0;$a<count($agent_list_2);$a++){
						$list[$i]['people_2'] += $user->where("t_id = {$agent_list_2[$a]['id']}")->count();
						$list[$i]['add_points_2'] += $integral->where("agent_id = {$agent_list_2[$a]['id']} && type = 1")->sum('points');
						$list[$i]['del_points_2'] += $integral->where("agent_id = {$agent_list_2[$a]['id']} && type = 0")->sum('points');
						$list[$i]['add_2'] += $order->where("tid = {$agent_list_2[$a]['id']}")->sum('add_points');
						$list[$i]['del_2'] += $order->where("tid = {$agent_list_2[$a]['id']}")->sum('del_points');
					}
					$list[$i]['people_agent'] = count($agent_list_2);
					$list[$i]['people'] = $list[$i]['people_2']+$user->where("t_id = {$list[$i]['id']}")->count();
					$list[$i]['add_points'] = $list[$i]['add_points_2']+$integral->where("agent_id = {$list[$i]['id']} && type = 1")->sum('points');
					$list[$i]['del_points'] = $list[$i]['del_points_2']+$integral->where("agent_id = {$list[$i]['id']} && type = 0")->sum('points');
					$list[$i]['add'] = $list[$i]['add_2']+$order->where("tid = {$list[$i]['id']}")->sum('add_points');
					$list[$i]['del'] = $list[$i]['del_2']+$order->where("tid = {$list[$i]['id']}")->sum('del_points');
					$list[$i]['fan'] = ((C('agent_1')+C('agent_2'))*($order->where("tid = {$list[$i]['id']}")->sum('del_points'))+C('agent_1')*$list[$i]['del_2'])*0.01;
				}else{
					$list[$i]['people'] = $user->where("t_id = {$list[$i]['id']}")->count();
					$list[$i]['add_points'] = $integral->where("agent_id = {$list[$i]['id']} && type = 1")->sum('points');
					$list[$i]['del_points'] = $integral->where("agent_id = {$list[$i]['id']} && type = 0")->sum('points');
					$list[$i]['add'] = $order->where("tid = {$list[$i]['id']}")->sum('add_points');
					$list[$i]['del'] = $order->where("tid = {$list[$i]['id']}")->sum('del_points');
					$list[$i]['fan'] = C('agent_2')*$list[$i]['del']*0.01;
				}
			}
		}else{
			if($admin['agent_rank']==1){//一级代理
				
				$count = $agent->where("id = {$admin['id']} || agent_up = {$admin['id']}")->count();
				$page = new \Think\Page($count,10);
				$show = $page->show();
				$list = $agent->where("id = {$admin['id']} || agent_up = {$admin['id']}")->limit($page->firstRow.','.$page->listRows)->select();

				for($i=0;$i<count($list);$i++){
					if($list[$i]['agent_rank']==1){
						$agent_list_2 = $agent->where("agent_up = {$list[$i]['id']}")->select();
						for($a=0;$a<count($agent_list_2);$a++){
							$list[$i]['people_2'] += $user->where($map)->where("t_id = {$agent_list_2[$a]['id']}")->count();
							$list[$i]['add_points_2'] += $integral->where($map)->where("agent_id = {$agent_list_2[$a]['id']} && type = 1")->sum('points');
							$list[$i]['del_points_2'] += $integral->where($map)->where("agent_id = {$agent_list_2[$a]['id']} && type = 0")->sum('points');
							$list[$i]['add_2'] += $order->where($map)->where("tid = {$agent_list_2[$a]['id']}")->sum('add_points');
							$list[$i]['del_2'] += $order->where($map)->where("tid = {$agent_list_2[$a]['id']}")->sum('del_points');
						}
						$list[$i]['people_agent'] = count($agent_list_2);
						$list[$i]['people'] = $list[$i]['people_2']+$user->where($map)->where("t_id = {$list[$i]['id']}")->count();
						$list[$i]['add_points'] = $list[$i]['add_points_2']+$integral->where($map)->where("agent_id = {$list[$i]['id']} && type = 1")->sum('points');
						$list[$i]['del_points'] = $list[$i]['del_points_2']+$integral->where($map)->where("agent_id = {$list[$i]['id']} && type = 0")->sum('points');
						$list[$i]['add'] = $list[$i]['add_2']+$order->where($map)->where("tid = {$list[$i]['id']}")->sum('add_points');
						$list[$i]['del'] = $list[$i]['del_2']+$order->where($map)->where("tid = {$list[$i]['id']}")->sum('del_points');
						$list[$i]['fan'] = ((C('agent_1')+C('agent_2'))*($order->where("tid = {$list[$i]['id']}")->sum('del_points'))+C('agent_1')*$list[$i]['del_2'])*0.01;
					}else{
						$list[$i]['people'] = $user->where($map)->where("t_id = {$list[$i]['id']}")->count();
						$list[$i]['add_points'] = $integral->where($map)->where("agent_id = {$list[$i]['id']} && type = 1")->sum('points');
						$list[$i]['del_points'] = $integral->where($map)->where("agent_id = {$list[$i]['id']} && type = 0")->sum('points');
						$list[$i]['add'] = $order->where($map)->where("tid = {$list[$i]['id']}")->sum('add_points');
						$list[$i]['del'] = $order->where($map)->where("tid = {$list[$i]['id']}")->sum('del_points');
						$list[$i]['fan'] = C('agent_2')*$list[$i]['del']*0.01;
					}
				}
			}else{
				$list['id'] = $admin['id'];
				$list['nickname'] = $admin['nickname'];
				$list['people'] = $user->where($map)->where("t_id = {$admin['id']}")->count();
				$list['add_points'] = $integral->where($map)->where("agent_id = {$admin['id']} && type = 1")->sum('points');
				$list['del_points'] = $integral->where($map)->where("agent_id = {$admin['id']} && type = 0")->sum('points');
				$list['add'] = $order->where($map)->where("tid = {$admin['id']}")->sum('add_points');
				$list['del'] = $order->where($map)->where("tid = {$admin['id']}")->sum('del_points');
				$list['fan'] = C('agent_2')*$list['del']*0.01;
				$list['delpoints'] = $admin['delpoints'];
			}
		}
		$this->assign('list1',$list1);
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}
	
	
	public function xiajia(){
		$id = I('id');
		$user = M('user');
		$count = $user->where("t_id = $id")->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $user->where("t_id = $id")->limit($page->firstRow.','.$page->listRows)->select();
		
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}
	
	public function xiajia_agent(){
		$id = I('id');
		$user = M('agent');
		$count = $user->where("agent_up = $id")->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $user->where("agent_up = $id")->limit($page->firstRow.','.$page->listRows)->select();
		
		$this->assign('show',$show);
		$this->assign('list',$list);
		$this->display();
	}
	
	
	public function under(){
		if(IS_POST){
			if($_SESSION['admin']['role_id'] != 0){
				$this->error('没有权限');
			}
			$id = I('aid');
			$points = I('points');
			if($points>I('balance')){
				$this->error('余额不足');
			}
			$res = M('agent')->where("id = $id")->setInc('delpoints',$points);
			if($res){
				$this->success('成功',U('Agent/index'),1);
			}else{
				$this->error('失败');
			}
		}else{
			$id = I('id');
			$points = I('points');
			$userinfo = M('agent')->where("id = $id")->find();
			$this->assign('points',$points);
			$this->assign('userinfo',$userinfo);
			$this->display();
		}
	}
	
	
}


?>